package com.emar.adx.yiqifa.algorithm;

import java.util.Arrays;

/**
 * Created by xiaoliang
 * 2016/10/8 10:48
 *
 * @Version 1.0
 */
public class QuickSort {

    private int[] datas = SortConstants.DATA_ARRAYS;

    public void sort (){
        quickSort(datas,0,datas.length-1);
    }

    private void quickSort(int[] arrays , int lowIndex,int highIndex){
        if(lowIndex<highIndex){
            int i = lowIndex;
            int j = highIndex;
            int tmp = arrays[i];

            while (i<j){

                //// 从右向左找小于tmp的数来填arrays[i]
                for(;j>i;j--){
                    if(arrays[j]<tmp){
                        arrays[i] = arrays[j];
                        i++;
                        break;
                    }
                }

                // 从左向右找大于或等于tmp的数来填arrays[j]
                for(;i<j;i++){
                    if(arrays[i]>tmp){
                        arrays[j] = arrays[i];
                        j--;
                        break;
                    }
                }
            }
            arrays[i] = tmp;
            System.out.println(Arrays.toString(arrays));

            quickSort(arrays,lowIndex,i-1);
            quickSort(arrays,i+1,highIndex);
        }
    }



}
